module positions
	use pieces
	implicit none
	private

	public position_module_initialize, position_module_finalize
	public position, contents, clear, is_empty, assignment(=), as_string

	type position
		private
		type(piece):: contents
		logical:: empty = .true.
	end type position

	interface assignment(=)
		module procedure position_assign
	end interface

	interface clear
		module procedure position_clear
	end interface
	
	interface is_empty
		module procedure position_is_empty
	end interface

	interface as_string
		module procedure position_as_string
	end interface

contains

    subroutine position_module_finalize ()
    end subroutine position_module_finalize

    subroutine position_module_initialize ()
    end subroutine position_module_initialize

	elemental function contents(p)
		type(position), intent(in):: p
		type(piece):: contents
		contents = p%contents
	end function contents
	
	elemental function position_as_string(p)
		type(position), intent(in):: p
		character position_as_string
		if (is_empty(p)) then
			position_as_string = "-"
		else
			position_as_string = as_string(p%contents)
		endif
	end function position_as_string
	
	elemental function position_is_empty(p)
		type(position), intent(in):: p
		logical position_is_empty
		position_is_empty = p%empty
	end function position_is_empty

	subroutine position_assign(p, q)
		type(position), intent(inout):: p
		type(piece), intent(in):: q
		call clear(p)
		p%contents = q
		p%empty = .false.
	end subroutine position_assign

	subroutine position_clear (p)
		type(position), intent(inout):: p
		p%empty = .true.
	end subroutine position_clear

end module positions
